<?php
class FunctionGamePlay{
public static $static = null;  
	static function readGamePlay($gameid){
		$appid = DataGameManager::getAppID();
		$static_key = "_gplay_" .$gameid;
		$gplay = FunctionApp::getStaticKey($appid, $static_key);
		if($gplay != null)return DataGamePlay::swap(ByteBuffer::wrap( $gplay ) );
		return new DataGamePLay(0,0);
	}
	static function writeGamePlay($gameid, $gplay){
		$appid = DataGameManager::getAppID();
		$static_key = "_gplay_" .$gameid;
		FunctionApp::setStaticKey($appid, $static_key, $gplay->getBinary());
	}
	static function readGameTable($gameid, $tableid){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = "gctable_" .$gameid."_".$tableid;
		$table_data = FunctionGamePlay::$static ->get( $key ) ;
		if($table_data != null){
			return DataTable::swap(ByteBuffer::wrap( $table_data ) );
		}
		return new DataTable($gameid, $tableid);
	}
	static function writeGameTable($gameid, $tableid, $table_obj){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = "gctable_" .$gameid."_".$tableid;
		{if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $table_obj->getBinary() );else FunctionGamePlay::$static ->replace( $key ,  $table_obj->getBinary() );} ;
	}
	static function deleteGameTable($gameid, $tableid){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = "gctable_" .$gameid."_".$tableid;
		FunctionGamePlay::$static ->delete( $key ) ;
	}
	static function readTableOfUser($gameid, $userid){
		$appid = DataGameManager::getAppID();
		$static_key = "_gtable_" .$gameid;
		$id = FunctionApp.getUserKey($appid, $userid, $static_key);
		if($id != null)return $id;
		return -1;
	}
	static function writeTableOfUser($gameid, $userid, $tableid){
		$appid = DataGameManager::getAppID();
		$static_key = "_gtable_" .$gameid;
		FunctionApp.setUserKey($appid, $userid, $static_key, $tableid);
	}
	static function readTransactionData($gameid, $tableid, $transaction){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_trans_" . $gameid ."_".  $tableid ."_".  $transaction ) ;
		$data = FunctionGamePlay::$static ->get( $key ) ;
		return $data;
	}
	static function writeTransactionData($gameid, $tableid, $transaction, $data){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_trans_" . $gameid ."_".  $tableid ."_".  $transaction ) ;
		{if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $data );else FunctionGamePlay::$static ->replace( $key ,  $data );} ;
	}
	static function deleteTransactionData($gameid, $tableid, $transaction){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_trans_" . $gameid ."_".  $tableid ."_".  $transaction ) ;
		FunctionGamePlay::$static ->delete( $key ) ;
	}
	static function readServerTransaction($gameid, $tableid){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_svtrans_" . $gameid ."_".  $tableid ) ;
		$trans = FunctionGamePlay::$static ->get( $key ) ;
		return $trans;
	}
	static function writeServerTransaction($gameid, $tableid, $data){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = $key = ("gc_svtrans_" . $gameid ."_".  $tableid ) ;
		{if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $data );else FunctionGamePlay::$static ->replace( $key ,  $data );} ;
	}
	static function deleteServerTransaction($gameid, $tableid){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_svtrans_" . $gameid ."_".  $tableid ) ;
		FunctionGamePlay::$static ->delete( $key ) ;
	}
	static function readPlayerTransaction($gameid, $tableid, $seatid){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_strans_" . $gameid ."_".  $tableid ."_".  $seatid ) ;
		$data = FunctionGamePlay::$static ->get( $key ) ;
		return $data;
	}
	static function writePlayerTransaction($gameid, $tableid, $seatid, $data){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_strans_" . $gameid ."_".  $tableid ."_".  $seatid ) ;
		{if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $data );else FunctionGamePlay::$static ->replace( $key ,  $data );} ;
	}
	static function deletePlayerTransaction($gameid, $tableid, $seatid){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = ("gc_strans_" . $gameid ."_".  $tableid ."_".  $seatid ) ;
		FunctionGamePlay::$static ->delete( $key ) ;
	}
        static function writeUserDataTable($gameid,$userid ,$userdata){
                {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = trim(KEYOBJ_USERTABLE . $gameid ."_".  $userid) ;
		{if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $userdata );else FunctionGamePlay::$static ->replace( $key ,  $userdata );} ;
        }
        static function readUserDataTable($gameid,$userid){
            {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = trim(KEYOBJ_USERTABLE . $gameid ."_".  $userid) ;
		$user_data = FunctionGamePlay::$static ->get( $key ) ;          
		if($user_data != null){
			return DataUserTablePlay::swap(ByteBuffer::wrap( $user_data ) );
		}
		return new DataUserTablePlay($userid, $gameid, -1 , 0,intval(time()),-1);
        }
        static function deleteUserDataTable($gameid,$userid){
            {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = trim(KEYOBJ_USERTABLE . $gameid ."_".  $userid) ;
                FunctionGamePlay::$static ->delete( $key ) ;
        }
         static function writeInfoTable($gameid,$infotable){
              {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
              $key = trim("info_table_").$gameid ;
	      {if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $infotable );else FunctionGamePlay::$static ->replace( $key ,  $infotable );} ;
        }
        static function readInfoTable($gameid){
              {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
              $key = trim("info_table_").$gameid ;
                $infotable = FunctionGamePlay::$static ->get( $key ) ;
		if($infotable != null){
			return InfoTable::swap($infotable);
		}
		return new InfoTable(4, 4);
        }
        static function writeDataGame($id,$gamedata){
             {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
             $key = trim("gamedata_").$id ;
		{if(FunctionGamePlay::$static ->get( $key )==null)FunctionGamePlay::$static ->set( $key ,  $gamedata );else FunctionGamePlay::$static ->replace( $key ,  $gamedata );} ;
        }
        static function readDataGame($id){
              {if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;  
                $key = trim("gamedata_").$id ;
                $gamedata = FunctionGamePlay::$static ->get( $key ) ;
		if($gamedata != null){
			return DataGameTest::swap($gamedata);
		}
		return new DataGameTest(0,0,0,0,0);
        }
        static function deleteDataGame($id){
		{if(!isset(FunctionGamePlay::$static ) || FunctionGamePlay::$static ==null){FunctionGamePlay::$static  = new Memcache;FunctionGamePlay::$static ->connect("127.0.0.1" , 11214 ) or die ("Could not connect");};} ;
		$key = "gamedata_".$id;
		FunctionGamePlay::$static ->delete( $key ) ;
	}
}
?>